package com.zhx.real.security.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义失败处理器
 * 和成功处理器同样的逻辑
 * 完成后需要重新注入到config中
 *
 * 可以 实现 AuthenticationFailureHandler
 * 也可以直接继承 spring的默认失败处理逻辑器 SimpleUrlAuthenticationFailureHandler
 *
 * @author zhx
 * @create 2017-11-02 11:34
 **/
@Component("myAuthentionFailedHandler")
public class MyAuthentionFailedHandler extends SimpleUrlAuthenticationFailureHandler {
    @Bean
    @ConditionalOnMissingBean(name = "cuestomWebAuthentionFailedHandler")
    public CuestomAuthentionFailedHandler cuestomAuthentionFailedHandler(){
        return new DefaultCuestomAuthentionFailedHandler();
    }

    @Autowired
    private CuestomAuthentionFailedHandler cuestomAuthentionFailedHandler;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        cuestomAuthentionFailedHandler.onAuthenticationFailure(httpServletRequest,httpServletResponse,e);
    }
}
